package cn.deschen.search.mysql.handlers.impl;

import cn.deschen.search.constants.OpType;
import cn.deschen.search.index.IndexHandler;
import cn.deschen.search.index.adKeyword.AdKeywordObject;
import cn.deschen.search.mysql.BinlogRowData;
import cn.deschen.search.mysql.handlers.BinlogRowDataHandler;
import cn.deschen.search.mysql.utils.SerializableUtil;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.Map;

import static cn.deschen.search.constants.TableConstants.AdKeyword.*;

/**
 * @Author hanbin_chen
 * @Description 关键词
 * @DateTime 2021/6/15
 * @Version V1.0.0
 */
@Component
public class AdKeywordHandler implements BinlogRowDataHandler {

    @Override
    public void handlerBinlogRowData(BinlogRowData rowData) {
        for (Map<String, Serializable> fieldValueMap : rowData.getRowDataList()) {
            AdKeywordObject keywordObject = new AdKeywordObject();

            fieldValueMap.forEach((k, v) -> {
                switch (k) {
                    case COLUMN_ID:
                        keywordObject.setId(SerializableUtil.parse2Long(v));
                        break;
                    case COLUMN_KEYWORD_NAME:
                        keywordObject.setKeywordName(SerializableUtil.parse2String(v));
                        break;
                    case COLUMN_IS_DEL:
                        keywordObject.setIsDel(SerializableUtil.parse2Integer(v));
                        break;
                }
            });
            IndexHandler.handlerLevel2Index(keywordObject, OpType.to(rowData.getEventType()));
        }

    }
}
